package com.softeem.yuedong_server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.softeem.yuedong_server.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.softeem.yuedong_server.entity.User;

import com.softeem.yuedong_server.dto.Result;
import java.util.Objects;

@Tag(name = "用户管理")
@RequestMapping("/user")
@RestController
public class UserController extends BaseController{
    @Autowired
    private UserService userService;

    @Operation(summary = "用户注册",description = "用户注册，需要提供用户名，密码，性别，生日，头像，其中用户密码必须填写")
    @PostMapping("/reg")
    public Result reg(@RequestBody User user){
        return userService.registry(user);
    }

    @Operation(summary = "用户登录",description = "用户登录，需要提供用户名，密码")
    @Parameters({
            @Parameter(name = "username",description = "用户名，可以为手机号、邮箱或账号",required = true),
            @Parameter(name = "password",description = "密码",required = true)}
    )
    @PostMapping("/login")
    public Result login(@RequestParam("username") String username,@RequestParam("password") String password){
        LambdaQueryWrapper<User> query = Wrappers.lambdaQuery(User.class).eq(User::getUsername,username);//eq.("username",username)
        User user = userService.getOne(query);
        if(!Objects.nonNull(user)){
            return Result.fail().setMsg("账号不存在");
        }
        if(Objects.equals(user.getPassword(),password)){
            return Result.ok().setMsg("登入成功").setData(user);
        }
        return Result.fail().setMsg("密码错误");
    }
}
